package com.cac.demo.newCode.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cac.demo.newCode.materialStorage.dto.MaterialDTO;
import com.cac.demo.newCode.materialStorage.entity.MdsItem;
import com.cac.demo.newCode.materialStorage.entity.WmsDeliveryPlan;
import com.cac.demo.newCode.materialStorage.service.IMdsItemService;
import com.cac.demo.newCode.materialStorage.service.IWmsDeliveryPlanService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@Component
public class MaterialUtil {
    @Resource
    private IWmsDeliveryPlanService wmsDeliveryPlanService;
    @Resource
    private IMdsItemService mdsItemService;
    public MaterialDTO getMaterialDTO(String message){
        String[] split = message.split(" ");
        MaterialDTO materialDTO = new MaterialDTO();
        String str = message;
        String[] parts = str.split("\\*");

        materialDTO.setAoNo("");
        if (parts.length>1)
        {
            materialDTO.setAoNo(parts[0]);
            materialDTO.setMaterialType("Z");
            return materialDTO;
        }
        if (split.length>1){
            String supplierCode = split[2].substring(1);
            String standardCode = split[3].substring(1);
            String productModel = split[3].substring(1);
            String certificate = split[4].substring(1);
            materialDTO.setSupplierCode(supplierCode);
            materialDTO.setStandardCode(standardCode);
            materialDTO.setProductModel(productModel);
            materialDTO.setCertificate(certificate);
            LambdaQueryWrapper<WmsDeliveryPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmsDeliveryPlan::getMesCheckQualiticationId, materialDTO.getCertificate());
            List<WmsDeliveryPlan> wmsDeliveryPlans = wmsDeliveryPlanService.list(queryWrapper);
            WmsDeliveryPlan wmsDeliveryPlan = wmsDeliveryPlans.stream().findFirst().get();
            materialDTO.setBarCode(wmsDeliveryPlan.getBarcode());
            String mdsItemId = wmsDeliveryPlan.getMdsItemId();
            LambdaQueryWrapper<MdsItem> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MdsItem::getMdsItemId, mdsItemId);
            List<MdsItem> mdsItems = mdsItemService.list(queryWrapper1);
            MdsItem mdsItem = mdsItems.stream().findFirst().get();
            materialDTO.setMaterialType(mdsItem.getItemType());
        }else {
            WmsDeliveryPlan wmsDeliveryPlan = wmsDeliveryPlanService.getWmsDeliveryPlanByMessage(message);
            if (wmsDeliveryPlan==null)
            {
                return materialDTO;
            }
            String mdsItemId = wmsDeliveryPlan.getMdsItemId();
            LambdaQueryWrapper<MdsItem> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MdsItem::getMdsItemId, mdsItemId);
            List<MdsItem> mdsItems = mdsItemService.list(queryWrapper1);
            MdsItem mdsItem = mdsItems.stream().findFirst().get();
            materialDTO.setMaterialType(mdsItem.getItemType());
            //零件
            materialDTO.setBarCode(wmsDeliveryPlan.getBarcode());
            //成品
            materialDTO.setCertificate(wmsDeliveryPlan.getMesCheckQualiticationId());
            //标件
            materialDTO.setStandardCode(wmsDeliveryPlan.getAttribute3Desc());
        }
        return materialDTO;
    }
}
